package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.constant.BusinessStatusConstants;
import com.qk.common.util.CurrentUserContextHolders;
import com.qk.domain.business.BusinessDO;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.business.BusinessDTO;
import com.qk.dto.business.BusinessListDTO;
import com.qk.dto.business.BusinessPoolDTO;
import com.qk.dto.business.UpdateBusinessInfoDTO;
import com.qk.dto.customer.AddCustomerDTO;
import com.qk.dto.customer.CustomerPageDTO;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.entity.User;
import com.qk.management.mapper.BusinessMapper;
import com.qk.management.mapper.BusinessTrackRecordMapper;
import com.qk.management.mapper.UserMapper;
import com.qk.management.service.BusinessService;
import com.qk.vo.business.BusinessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Just、倾城
 * @created 2025-10-15
 * @description
 *
 */
@Slf4j
@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;

    @Autowired
    private UserMapper userMapper;




    @Override
    public PageResult<BusinessDO> listPool(BusinessPoolDTO dto) {
        Page<BusinessDO> page = new Page<>(dto.getPage(), dto.getPagesize());
        Page<BusinessDO> pageInfo = this.baseMapper.selectListPool(page,dto);
        return PageResult.<BusinessDO>builder().total(Integer.getInteger(pageInfo.getTotal() + "")).rows(pageInfo.getRecords()).build();
    }

    @Override
    public void deleteById(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(BusinessStatusConstants.RECYCLE);
        this.baseMapper.updateById(business);
    }

    @Override
    public void toCustomer(Integer id) {
        Business business = this.baseMapper.selectById(id);
        business.setUpdateTime(LocalDateTime.now());
        business.setStatus(BusinessStatusConstants.TRANSFER_CUSTOMER);
        this.baseMapper.updateById(business);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateBusinessInfoById(UpdateBusinessInfoDTO updateBusinessInfoDTO) {
            Business business = BeanUtil.copyProperties(updateBusinessInfoDTO, Business.class);
            business.setUpdateTime(LocalDateTime.now());
            if(ObjectUtil.equals(business.getStatus(), BusinessStatusConstants.WAIT_ALLOCATION)){
                business.setStatus(BusinessStatusConstants.WAIT_FOLLOW_UP);
            }
            this.baseMapper.updateById(business);
        BusinessTrackRecord businessTrackRecord = BeanUtil.copyProperties(updateBusinessInfoDTO, BusinessTrackRecord.class);
        businessTrackRecord.setId(null);
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserContextHolders.get());
        this.businessTrackRecordMapper.insert(businessTrackRecord);
    }

    @Override
    public BusinessVO getBusinessById(Integer id) {
        Business business =this.baseMapper.selectById(id);
        Integer userId = business.getUserId();
        User user = userMapper.selectById(userId);
        BusinessVO businessVO = BeanUtil.copyProperties(business, BusinessVO.class);
        List<BusinessTrackRecord> businessTrackRecords = businessTrackRecordMapper.selectList(Wrappers.lambdaQuery(BusinessTrackRecord.class)
                .in(BusinessTrackRecord::getBusinessId, id)
        );
        List<BusinessVO.BusinessTrackRecordVO> list = businessTrackRecords.stream().map(businessTrackRecord -> {
            BusinessVO.BusinessTrackRecordVO vo = BeanUtil.copyProperties(businessTrackRecord, BusinessVO.BusinessTrackRecordVO.class);
            vo.setAssignName(user.getUsername());
            return vo;
        }).toList();
        businessVO.setTrackRecords( list);
        return businessVO;
    }

    @Override
    public PageResult<BusinessDO> businessList(BusinessListDTO businessListDTO) {
        Page<BusinessDO> page = new Page<>(businessListDTO.getPage(), businessListDTO.getPagesize());
        Page<BusinessDO> pageInfo = this.baseMapper.businessList(page, businessListDTO);
        return PageResult.<BusinessDO>builder().total(Integer.getInteger(pageInfo.getTotal() + "")).rows(pageInfo.getRecords()).build();
    }

    @Override
    public void saveBusiness(BusinessDTO businessDTO) {
        Business business = BeanUtil.copyProperties(businessDTO, Business.class);
        business.setStatus(BusinessStatusConstants.WAIT_ALLOCATION);
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(business);
    }
}
